//
//  String+ Regular.swift
//  正则表达式
//
//  Created by wei_jingyun on 15/7/9.
//  Copyright © 2015年 wei_jingyun. All rights reserved.
//

import Foundation

extension String {

    /**
    传入正则表达式 返回 () 内容
    :param: regular let reg = "<a href=\"(.*?)\".*?>(.*?)</a>"
    :returns: 按顺序返回 () 内容
    */
    func regular(regular: String) -> [String] {
        
        var strA = [String]()
        
        do {
            let regex = try NSRegularExpression(pattern: regular, options: NSRegularExpressionOptions.DotMatchesLineSeparators)
            
            if let checkResult = regex.firstMatchInString(self, options: NSMatchingOptions(rawValue: 0), range: NSRange(location: 0, length: self.characters.count)) {
                
                // 从 1 开始 ， 0 是String本身
                for i in 1..<checkResult.numberOfRanges {
                    let range = checkResult.rangeAtIndex(i)
                    strA.append((self as NSString).substringWithRange(range))
                }
    
            }
        } catch {
            print(error)
        }
        return strA
    }
    
}
